<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>rnd31</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="rnd.html" title="rnd" />
    <link rel="next" href="round.html" title="round" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">rnd31</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="rnd.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="round.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="rnd31"></a>
      <div class="titlepage"></div>
      <a id="IndexRnd31" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">rnd31</span>
        </h2>
        <p>rnd31 — 
      31-bit bipolar random opcodes with controllable distribution.
          </p>
      </div>
      <div class="refsect1">
        <a id="idp143330912"></a>
        <h2>Description</h2>
        <p>
      31-bit bipolar random opcodes with controllable distribution. These units are portable, i.e. using the same seed value will generate the same random sequence on all systems. The distribution of generated random numbers can be varied at k-rate.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp143332656"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">ax <span class="command"><strong>rnd31</strong></span> kscl, krpow [, iseed]</pre>
        <pre class="synopsis">ix <span class="command"><strong>rnd31</strong></span> iscl, irpow [, iseed]</pre>
        <pre class="synopsis">kx <span class="command"><strong>rnd31</strong></span> kscl, krpow [, iseed]</pre>
      </div>
      <div class="refsect1">
        <a id="idp143399920"></a>
        <h2>Initialization</h2>
        <p>
      <span class="emphasis"><em>ix</em></span> -- i-rate output value.
    </p>
        <p>
      <span class="emphasis"><em>iscl</em></span> -- output scale. The generated random numbers are in the range -iscl to iscl.
    </p>
        <p>
      <span class="emphasis"><em>irpow</em></span> -- controls the distribution of random numbers. If irpow is positive, the random distribution (x is in the range -1 to 1) is <span class="emphasis"><em>abs(x) ^ ((1 / irpow) - 1)</em></span>; for negative irpow values, it is <span class="emphasis"><em>(1 - abs(x)) ^ ((-1 / irpow) - 1)</em></span>. Setting <span class="emphasis"><em>irpow</em></span> to -1, 0, or 1 will result in uniform distribution (this is also faster to calculate).  
    </p>
        <p>
      </p>
        <div class="mediaobject">
          <img src="images/rnd31_rand.png" alt="[A graph of distributions for different values of irpow.]" />
          <div class="caption">
            <p>A graph of distributions for different values of irpow.</p>
          </div>
        </div>
        <p>
    </p>
        <p>
      <span class="emphasis"><em>iseed</em></span> (optional, default=0) -- seed value for random number generator (positive integer in the range 1 to 2147483646 (2 ^ 31 - 2)). Zero or negative value seeds from current time (this is also the default). Seeding from current time is guaranteed to generate different random sequences, even if multiple random opcodes are called in a very short time.
    </p>
        <p> In the a- and k-rate version the seed is set at opcode initialization. With i-rate output, if iseed is zero or negative, it will seed from current time in the first call, and return the next value from the random sequence in successive calls; positive seed values are set at all i-rate calls.  The seed is local for a- and k-rate, and global for i-rate units.
    </p>
        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
          <table border="0" summary="Note: Notes">
            <tr>
              <td rowspan="2" align="center" valign="top" width="25">
                <img alt="[Note]" src="images/note.png" />
              </td>
              <th align="left">Notes</th>
            </tr>
            <tr>
              <td align="left" valign="top">
                <p>
        </p>
                <div class="itemizedlist">
                  <ul class="itemizedlist" style="list-style-type: disc; ">
                    <li class="listitem">
                      <p>although seed values up to 2147483646 are allowed, it is recommended to use smaller numbers (&lt; 1000000) for portability, as large integers may be rounded to a different value if 32-bit floats are used.</p>
                    </li>
                    <li class="listitem">
                      <p>i-rate <span class="emphasis"><em>rnd31</em></span> with a positive seed will always produce the same output value (this is not a bug). To get different values, set seed to 0 in successive calls, which will return the next value from the random sequence.</p>
                    </li>
                  </ul>
                </div>
                <p>
      </p>
              </td>
            </tr>
          </table>
        </div>
      </div>
      <div class="refsect1">
        <a id="idp143415360"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>ax</em></span> -- a-rate output value.
    </p>
        <p>
      <span class="emphasis"><em>kx</em></span> -- k-rate output value.
    </p>
        <p>
      <span class="emphasis"><em>kscl</em></span> -- output scale. The generated random numbers are in the range -kscl to kscl. It is the same as <span class="emphasis"><em>iscl</em></span>, but can be varied at k-rate.
    </p>
        <p>
      <span class="emphasis"><em>krpow</em></span> -- controls the distribution of random numbers. It is the same as <span class="emphasis"><em>irpow</em></span>, but can be varied at k-rate.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp143420816"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the rnd31 opcode at a-rate. It uses the file <a class="ulink" href="examples/rnd31.csd" target="_top"><em class="citetitle">rnd31.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp143422688"></a>
          <p class="title">
            <strong>Example 770. An example of the rnd31 opcode at a-rate.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o rnd31.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="comment">; Initialize the global variables.</span>
<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 1

<span class="comment">; Instrument #1.</span>
<span class="oblock">instr</span> 1
  <span class="comment">; Create random numbers at a-rate in the range -2 to 2 with </span>
  <span class="comment">; a triangular distribution, seed from the current time.</span>
  a31 <span class="opc">rnd31</span> 2, <span class="op">-</span>0.5

  <span class="comment">; Use the random numbers to choose a frequency.</span>
  afreq <span class="op">=</span> a31 <span class="op">*</span> 500 <span class="op">+</span> 100

  a1 <span class="opc">oscil</span> 30000, afreq, 1
  <span class="opc">out</span> a1
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="comment">; Table #1, a sine wave.</span>
<span class="stamnt">f</span> 1 0 16384 10 1

<span class="comment">; Play Instrument #1 for one second.</span>
<span class="stamnt">i</span> 1 0 1
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
        <p>
      Here is an example of the rnd31 opcode at k-rate. It uses the file <a class="ulink" href="examples/rnd31_krate.csd" target="_top"><em class="citetitle">rnd31_krate.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp143428048"></a>
          <p class="title">
            <strong>Example 771. An example of the rnd31 opcode at k-rate.</strong>
          </p>
          <div class="example-contents">
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o rnd31_krate.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="comment">; Initialize the global variables.</span>
<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 1

<span class="comment">; Instrument #1.</span>
<span class="oblock">instr</span> 1
  <span class="comment">; Create random numbers at k-rate in the range -1 to 1 </span>
  <span class="comment">; with a uniform distribution, seed=10.</span>
  k1 <span class="opc">rnd31</span> 1, 0, 10
        
  <span class="opc">printks</span> "k1<span class="op">=</span><span class="op">%</span>f\\n", 0.1, k1
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="comment">; Play Instrument #1 for one second.</span>
<span class="stamnt">i</span> 1 0 1
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />

      Its output should include lines like this:
      </p>
        <pre class="screen">
k1=0.112106
k1=-0.274665
k1=0.403933
      </pre>
        <p>
    </p>
        <p>
      Here is an example of the rnd31 opcode that uses the number 7 as a seed value. It uses the file <a class="ulink" href="examples/rnd31_seed7.csd" target="_top"><em class="citetitle">rnd31_seed7.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp143431888"></a>
          <p class="title">
            <strong>Example 772. An example of the rnd31 opcode that uses the number 7 as a seed value.</strong>
          </p>
          <div class="example-contents">
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o rnd31_seed7.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="comment">; Initialize the global variables.</span>
<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 1

<span class="comment">; Instrument #1.</span>
<span class="oblock">instr</span> 1
  <span class="comment">; i-rate random numbers with linear distribution, seed=7. </span>
  <span class="comment">; (Note that the seed was used only in the first call.)</span>
  i1 <span class="opc">rnd31</span> 1, 0.5, 7
  i2 <span class="opc">rnd31</span> 1, 0.5
  i3 <span class="opc">rnd31</span> 1, 0.5
        
  <span class="opc">print</span> i1
  <span class="opc">print</span> i2
  <span class="opc">print</span> i3
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="comment">; Play Instrument #1 for one second.</span>
<span class="stamnt">i</span> 1 0 1
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />

      Its output should include lines like this:
      </p>
        <pre class="screen">
instr 1:  i1 = -0.649
instr 1:  i2 = -0.761
instr 1:  i3 = 0.677
      </pre>
        <p>
    </p>
        <p>
      Here is an example of the rnd31 opcode that uses the current time as a seed value. It uses the file <a class="ulink" href="examples/rnd31_time.csd" target="_top"><em class="citetitle">rnd31_time.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp143435696"></a>
          <p class="title">
            <strong>Example 773. An example of the rnd31 opcode that uses the current time as a seed value.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o rnd31_time.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="comment">; Initialize the global variables.</span>
<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 1

<span class="comment">; Instrument #1.</span>
<span class="oblock">instr</span> 1
  <span class="comment">; i-rate random numbers with linear distribution,</span>
  <span class="comment">; seeding from the current time. (Note that the seed </span>
  <span class="comment">; was used only in the first call.)</span>
  i1 <span class="opc">rnd31</span> 1, 0.5, 0
  i2 <span class="opc">rnd31</span> 1, 0.5
  i3 <span class="opc">rnd31</span> 1, 0.5

  <span class="opc">print</span> i1
  <span class="opc">print</span> i2
  <span class="opc">print</span> i3
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="comment">; Play Instrument #1 for one second.</span>
<span class="stamnt">i</span> 1 0 1
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />

      Its output should include lines like this:
      </p>
        <pre class="screen">
instr 1:  i1 = -0.691
instr 1:  i2 = -0.686
instr 1:  i3 = -0.358
      </pre>
        <p>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp143440784"></a>
        <h2>Credits</h2>
        <p>Author: Istvan Varga</p>
        <p>New in version 4.16</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="rnd.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="round.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">rnd </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> round</td>
        </tr>
      </table>
    </div>
  </body>
</html>
